﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
using System.Globalization;

namespace Poligono.IO.FormatTypes
{
    /// <summary>
    /// Provider Csv to helper maipulate file in Csv format
    /// </summary>
    public static class CsvHelper
    {
        /// <summary>
        /// Get a Datatable with date of Csv file
        /// </summary>
        /// <remarks>the sperate between columns is ","</remarks>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static DataTable ObterDataTable(string filePath)
        {
            return GetDataTable(filePath, ",", true);
        }


        /// <summary>
        /// Get a Datatable with date of Csv file, using "separator" like as sperate between columns
        /// </summary>
        /// <param name="filePath">path to file, The file will be tranforned in a DataTable</param>
        /// <param name="separator">Separador de colunas no arquivo texto</param> 
        /// <param name="hasHeader">Indica se a primeira linha do arquivo contem os nomes das colunas</param>
        /// <returns></returns>
        public static DataTable GetDataTable(string filePath, string separator, bool hasHeader)
        {
            using (StreamReader stream = new StreamReader(filePath, Encoding.Default))
            {
                return GetDataTable(stream.BaseStream, separator, hasHeader);
            }
        }

        /// <summary>
        /// Get a Datatable with date of Csv file, using separator like as sperate between columns
        /// </summary>
        /// <param name="stream">file Csv in stream format</param>
        /// <param name="separator">separet between columns</param>
        /// <param name="hasHeader">indicate value used to split date/columns</param>
        /// <returns></returns>
        public static DataTable GetDataTable(Stream stream, string separator, bool hasHeader)
        {
            StreamReader file = new StreamReader(stream);
            bool isHeaderRow = hasHeader;
            DataTable result = new DataTable();
            //result.
            while (!file.EndOfStream)
            {
                string[] column = file.ReadLine().Split(new string[] { separator }, StringSplitOptions.None);
                if (isHeaderRow)
                {
                    foreach (string coluna in column)
                    {
                        result.Columns.Add(coluna);
                    }
                    // next row isen't Header
                    isHeaderRow = false;
                }
                else
                {
                    // if row has more column than header, ADD
                    while (result.Columns.Count < column.Length)
                    {
                        result.Columns.Add();
                    }

                    DataRow row = result.NewRow();
                    for (int i = 0; i < column.Length; i++)
                    {
                        row[i] = column[i];
                    }
                    result.Rows.Add(row);
                }
            }
            return result;
        }
    }
}
